import 'package:easy_refresh/easy_refresh.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/activity/super_activity_model.dart';

class MyActivityPageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final _pageSize = 20;
  List<SuperActivityModel> activityList = List<SuperActivityModel>.of([]);

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    List<SuperActivityModel>? resp =
        await Api.getMyParticipated(page: page, pageSize: _pageSize);
    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    List<SuperActivityModel>? resp =
        await Api.getMyParticipated(page: page, pageSize: _pageSize);
    if (resp == null) {
      return IndicatorResult.fail;
    }
    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();
    return IndicatorResult.success;
  }

  void _setList(List<SuperActivityModel>? list) {
    activityList.clear();
    _addList(list);
  }

  void _addList(List<SuperActivityModel>? list) {
    if (null != list && list.isNotEmpty) {
      activityList.addAll(list);
      update();
    }
  }
}
